package com.zay.a5_buttons;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.zay.a5_buttons.fragment.FindFragment;
import com.zay.a5_buttons.fragment.IndexFragment;
import com.zay.a5_buttons.fragment.MessageFragment;
import com.zay.a5_buttons.fragment.MineFragment;

public class FragmentActivity extends AppCompatActivity {
    private IndexFragment indexFragment;
    private FindFragment findFragment;
    private MessageFragment messageFragment;
    private MineFragment mineFragment;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        FrameLayout main_frame=findViewById(R.id.main_frame);
        Button btn_index=findViewById(R.id.btn_index);
        Button btn_find=findViewById(R.id.btn_find);
        Button btn_message=findViewById(R.id.btn_message);
        Button btn_mine=findViewById(R.id.btn_mine);
        btn_index.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fragmentManager=getSupportFragmentManager();
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                hideFragment(fragmentTransaction);
                if(indexFragment == null){
                    indexFragment = new IndexFragment();
                    fragmentTransaction.add(R.id.main_frame,indexFragment);
                }else {
                    fragmentTransaction.show(indexFragment);
                }
                fragmentTransaction.commit();
            }
        });
        btn_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fragmentManager=getSupportFragmentManager();
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                hideFragment(fragmentTransaction);
                if(findFragment == null){
                    findFragment = new FindFragment();
                    fragmentTransaction.add(R.id.main_frame,findFragment);
                }else {
                    fragmentTransaction.show(findFragment);
                }
                fragmentTransaction.commit();
            }
        });
        btn_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fragmentManager=getSupportFragmentManager();
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                hideFragment(fragmentTransaction);
                if(messageFragment == null){
                    messageFragment = new MessageFragment();
                    fragmentTransaction.add(R.id.main_frame,messageFragment);
                }else {
                    fragmentTransaction.show(messageFragment);
                }
                fragmentTransaction.commit();
            }
        });
        btn_mine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fragmentManager=getSupportFragmentManager();
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                hideFragment(fragmentTransaction);
                if(mineFragment == null){
                    mineFragment = new MineFragment();
                    fragmentTransaction.add(R.id.main_frame,mineFragment);
                }else {
                    fragmentTransaction.show(mineFragment);
                }
                fragmentTransaction.commit();
            }
        });
    }
    private void hideFragment(FragmentTransaction transaction){
        if(indexFragment != null){
            transaction.hide(indexFragment);
        }
        if(findFragment != null){
            transaction.hide(findFragment);
        }
        if(messageFragment != null){
            transaction.hide(messageFragment);
        }
        if(mineFragment != null){
            transaction.hide(mineFragment);
        }
    }
}
